/* DIR_tree: (un)packet&&lzw_(de)copmress
	Name: DIR_tree: (un)packet&&lzw_(de)copmress
	Copyright: by DMSJ
	Author: DMSJ
	Date: 30/03/25 22:51
	Description: 树/串/表/栈.
*/
#include "mix.h"
//由于EGE库的原因,源文件_Gui.cpp_无法编入libMix.a中,故其需单独拿出编译[即放入项目中].

int main(int argc, char *argv[])
{
	int flag_error;

	//进入gui界面
	if (argc == 1)
	{
		fprintf(stdout, "\n命令行用法查看方式：%s I \n", argv[0]),
		        fprintf(stdout, "本次使用GUI界面\n\n");
		//进入gui界面
		GUI_run();
		return 1;
	}

	/*
	命令行方式
	*/
	// 转换所有参数=>宽字符型,作为备用。
	wchar_t* paths[2];
	foreach(i,2,0)
	{
		paths[i] = AgrvToWide(argv[i+2],-1);
		if (!paths[i])
		{
			// 清理已分配的内存
			foreach(j,i,0)
				free(paths[j]);

			if('i' == argv[1][0] || argv[1][0]  == 'I' )
				break;

			fprintf(stderr, "Failed to convert path after: %s\n", argv[i+1]);
			goto flag_error;
		}
	}
	//kernel
	switch(argv[1][0])
	{
		case 'p': //只packet
			pack_directory(paths[0],paths[1],'p');
			break;
		case 'd': //只de_packet
			unpack_directory(paths[0],paths[1],'d');
			break;

		case 'P'://packet+文件压缩
			pack_directory(paths[0],paths[1],'P');
			break;
		case 'D'://de_packet+文件解压（de_compress）
			unpack_directory(paths[0],paths[1],'D');
			break;

		case 'u'://packet + compress -一步到位=> 参数：dir | *.lzw
		{
			pack_directory(paths[0],L".\\hasty_create.pack",'p');

			fileopen(input,"hasty_create.pack","rb");
			fileopen(output,argv[3],"wb");

			lzw_compress(input, output), fclose_all(argv[3]);
		}
		break;
		case 'U'://de_packet + de_compress -一步到位=> 参数：*.lzw | dir
		{
			fileopen(input,argv[2],"rb");
			fileopen(output,"re_create.pack","wb");

			lzw_decompress(input, output), fclose_all("re_create.pack");

			unpack_directory(L".\\re_create.pack", paths[1],'d');
			/* TODO (dmsj#1#): 一个很神奇的问题^-^: 在unpacket
			                   +decompress -一步到位的情况下,最
			                   终生成的文件夹能容纳的大小只有
			                   179kb,和文件名又没关系

							   解决了，蛮难甭的，忘记fclose,有点心疼了【哭~.~】
							    */
		}
		break;

		case 'c'://compress
		{
			fileopen(input,argv[2],"rb");
			fileopen(output,argv[3],"wb");

			lzw_compress(input, output), fclose_all(argv[3]);
		}
		break;
		case 'C'://decompress
		{
			fileopen(input,argv[2],"rb");
			fileopen(output,argv[3],"wb");

			lzw_decompress(input, output), fclose_all(argv[3]);
		}
		break;

		//xor_format 加/解密一致
		case 'x'://加密
		case 'X'://解密
		{
			char password[MAX_PASSWORD_LEN];
			printf("!_Caution_!：请确保加/解密_code_一致\n"), printf("请输入密码[MAX: 100]："),
			       fgets(password, MAX_PASSWORD_LEN, stdin);

			password[strcspn(password, "\n")] = '\0'; // 去除换行符
			//加/解密
			process_file(argv[2],argv[3],password);
		}
		break;

flag_error:
		default:
			fprintf(stderr, "\n无效操作: 命令行界面用法如下:\n");
		case 'i':
		case 'I':
			fprintf(stdout, "\n命令行用法：%s <选项> <输入> <输出>\n", argv[0]);
			printf("<选项> :\n");
			printf("		p/d : packet/unpacket\n");
			printf("		P/D : packet+file_compress/unpacket+file_decompress => [打/解包过程中，对文件压缩/解压]\n");
			printf("		u/U : packet+packet_compress/packet_decompress+unpacket => [对（打包文件）压缩/解压]\n");
			printf("		c/C : compress/decompress\n");
			printf("		i/I : info about this program\n");
			printf("		x/X : encrypt/decipher\n");
			printf("<输入> : dir or file\n");
			printf("<输出> : 自定义格式，例如： demo.pack test.lzw .\\cjdkc\\ 等\n");
			printf(" [注]  : 因目前版本还未为加入default<输出>格式,所以不能省略为空，请谅解");

			return -1;

	}
	/* 改用switch_case优化.
		if (argv[1][0] == 'p')
			pack_directory(paths[0],paths[1]);
		else if (argv[1][0] == 'd')
			unpack_directory(paths[0],paths[1]);
		else
			fprintf(stderr, "无效操作，使用 p（打包）或 d（解包）\n");
	*/
	// formal_清理已分配的内存
	foreach(i,2,0)
		free(paths[i]);

	return 0;

}
